from zope.component import getUtility
from kss.core import KSSView, kssaction
from my315ok.flowrate import pkopenw,pkopenr
from zope.component import getMultiAdapter
from Products.CMFCore.utils import getToolByName
from Products.CMFPlone.interfaces import IPloneSiteRoot
from my315ok.flowrate.browser.controlpanel import IFlowRateSchema



class addaccessView(KSSView):     

    @kssaction
    def addaccess(self):
#        import pdb
#        pdb.set_trace()

        sitename = self.prefs.sitename
        import cPickle
        f = pkopenr('links.pkl')
        links = cPickle.load(f)        
        f.close()       
        
        if sitename in links.keys():
             links[sitename] = int(links[sitename]) + 1
        else:
            links[sitename] = 1                 
             
        fw = pkopenw('links.pkl')
        cPickle.dump(links,fw)
        fw.close()
        content = str(links[sitename])
        del links
       
                        
        # KSS specific calls
        core = self.getCommandSet('core')
        core.replaceInnerHTML('#flow-total', content)
        
    @property
    def prefs(self):
        portal = getUtility(IPloneSiteRoot)
        return IFlowRateSchema(portal)

        
        
    
    

        
        


